﻿@{
    ViewBag.Title = "流程设计器";
    Layout = "~/Views/Shared/_FlowForm.cshtml";
}
<script>
    var keyValue = request('keyValue');
    var postData = {};//提交数据
    var flowData = {};
    var frmData = {};
    var SchemeContentOld;
    var frmapp;
    var shcemeAuthorizeData = "";
    var AllAuthorizeCheckData = {};
    $(function () {
        initialPage();
    })
    //初始化页面
    function initialPage() {
        $('#step-1 .panel-body').height($(window).height() - 228);
        $('#Description').height($(window).height() - 385);
        $('#DesignateMemberlist').height($(window).height() - 141);
        $('#Treebackground').height($(window).height() - 131);
        $('#step-2 .tab-content').height($(window).height() - 167);
        initFrmInfo();
        initFlowInfo();
        //加载导向
        $('#wizard').wizard().on('change', function (e, data) {
            var $finish = $("#btn_finish");
            var $next = $("#btn_next");
            if (data.direction == "next") {
                switch (data.step) {
                    case 1:
                        if (!bindingBase())
                        {
                            return false;
                        }
                        break;
                    case 2://绑定表单
                       
                            if (!bindingFrm()) {
                                dialogTop("请选择左侧表单", "error");
                                return false;
                            }
                            var frmcotentls = frmapp.getData();
                            if (!frmcotentls) {
                                return false;
                            }
                            frmData.FrmContent = JSON.stringify(frmcotentls);
                        break;
                    case 3://流程设计
                        if (!bindingFlow())
                        {
                            return false;
                        }
                        $finish.removeAttr('disabled');
                        $next.attr('disabled', 'disabled');
                        $('#btn_caogao').attr('disabled', 'disabled');
                        break;
                    default:
                        break;
                }
            } else {
                $finish.attr('disabled', 'disabled');
                $next.removeAttr('disabled');
                $('#btn_caogao').removeAttr('disabled');
            }

        });
        //获取表单
        if (!!keyValue) {
            $.SetForm({
                url: "../../FlowManage/FlowDesign/GetFormJson",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#step-1").SetWebControls(data.schemeinfo);
                    postData["SchemeVersion"] = data.schemeinfo.SchemeVersion;

                    if(data.schemeinfo.EnabledMark == 3)
                    {
                        flowData["SchemeVersion"] = "cg";
                    }
                    SchemeContentOld = JSON.parse(data.schemecontent.SchemeContent);
                  
                        $('#FormFrmTree').setNodeChecked(SchemeContentOld.Frm.FrmId);
                        frmData.FrmId = SchemeContentOld.Frm.FrmId;
                        setFrmInfo(SchemeContentOld.Frm);
                   
                    setFlowInfo(SchemeContentOld.Flow);
                }
            });
        }


        //保存为草稿
        $('#btn_caogao').click(function () {
            if (!$('#step-1').Validform()) {
                return false;
            }
            var _data = $("#step-1").GetWebControls(keyValue);
            var _frmcotentls = "";
            if (postData.FrmType == 1) {
                frmData = "";
            }
            else {
                if (frmapp != undefined) {
                    _frmcotentls = frmapp.getData(true);
                }
                frmData.FrmContent = JSON.stringify(_frmcotentls);
            }

            flowData["SchemeContent"] = JSON.stringify({ "Frm": frmData, "Flow": FlowDesignPanel.exportData() });
            postData = $.extend(postData, _data);
            postData["EnabledMark"] = 3;
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            $.SaveForm({
                url: "../../FlowManage/FlowDesign/SaveForm?keyValue=" + keyValue,
                param: { "InfoEntity": JSON.stringify(postData), "ContentEntity": JSON.stringify(flowData), "shcemeAuthorizeData": shcemeAuthorizeData },
                loading: "正在保存数据...",
                success: function () {
                    parent.layer.close(index);
                }
            })
        });
    }
    /*=========基本配置（begin）==================================================================*/
    function bindingBase()
    {
        if (!$('#step-1').Validform()) {
            return false;
        }
        var _postData = $("#step-1").GetWebControls(keyValue);
        postData = $.extend(postData, _postData);
        return true;
    }
    /*=========基本配置（end）====================================================================*/

    /*=========表单选择（begin）==================================================================*/
    var _frmdatabase = "";
    var _frmflag = false;
    function initFrmInfo()
    {
        //加载左边的树
        var item = {
            height: $(window).height() - 87,
            url: "../../FlowManage/FormDesign/GetTreeJson",
            onnodeclick: function (item) {
                if (item.Sort == "Frm") {
                    frmData.FrmId = item.id;
                    if (SchemeContentOld != undefined && frmData.FrmId == SchemeContentOld.Frm.FrmId) {
                        setFrmInfo(SchemeContentOld.Frm);
                    }
                    else {
                        $.SetForm({
                            url: "../../FlowManage/FormDesign/GetFormJson",
                            param: { keyValue: item.id },
                            success: function (data) {
                                setFrmInfo(data);
                            }
                        });
                    }
                }
                else {
                    $('#frmdesign').hide();
                    $('#frmpreview').hide();
                    $('#frmDefaulting').show();
                }
            }
        };
        $("#FormFrmTree").treeview(item);

        $(".editview").hover(function () {
            $(this).find('.editviewtitle').show();
        }, function (e) {
            $(this).find('.editviewtitle').hide();
        });
        $('#editfrm').click(function () {
            if (_frmflag) {
                var frmcotentls = frmapp.getData();
                if (!frmcotentls) {
                    return false;
                }
                frmData.FrmContent = JSON.stringify(frmcotentls);
                $('#frmdesign').hide();
                $('#frmDefaulting').hide();
                $('#frmpreview').show();

                $('#frmpreview').frmPreview({
                    tablecotent: frmData.FrmContent,
                    width: 870
                });
                $(this).html("编辑表单");
                _frmflag = false;
            }
            else {
                if ( frmData.FrmName != undefined) {
                    $('#frmpreview').hide();
                    $('#frmDefaulting').hide();
                    $('#frmdesign').show();
                    frmapp = $('#frmdesign').frmDesign({
                        Height: $(window).height() - 179,
                        frmContent: frmData.FrmContent
                    });
                    _frmflag = true;
                    $(this).html("预览表单");
                }
                else {
                    dialogTop("请选择左侧表单", "error");
                }
            }
            return false;
        });
    }
    //设置表单数据
    function setFrmInfo(data)
    {
        $('#frmdesign').hide();
        $('#frmDefaulting').hide();
        $('#frmpreview').show();

        frmData.FrmDbId = data.FrmDbId;
        frmData.FrmTable = data.FrmTable;
        frmData.FrmName = data.FrmName;
        frmData.FrmContent = data.FrmContent;
        frmData.FrmTableId = data.FrmTableId;

        _frmflag = false;
        $('#frmpreview').frmPreview({
            tablecotent: data.FrmContent,
            width:870
        });
        

            frmapp = $('#frmdesign').frmDesign({
                Height: 480,
                frmContent: frmData.FrmContent
            });
        
    }
    function bindingFrm() {
        if (frmData.FrmName == undefined)
        {
            return false;
        }
        return true;
    }
    /*=========表单选择（end）====================================================================*/

    /*=========流程设计（begin）==================================================================*/
    var FlowDesignPanel;
    var FlowDesignObject;//
    var LineId;
    var nodePramData = [];
    function initFlowInfo()
    {
        FlowDesignPanel = $('#FlowPanel').flowdesign({
            height: ($(window).height() - 87),
            widht: 1000,
            OpenNode: function (object) {
                FlowDesignObject = object;
                if (object.$nodeData[object.$focus].type == 'startround')
                {
                    return false;
                }

                layer.open({
                    type: 2,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['800px', '450px'], //宽高
                    maxmin: true, //开启最大化最小化按钮
                    title: '节点设置【' + object.$nodeData[object.$focus].name + '】',
                    content: '/FlowManage/FlowDesign/FlowNodeForm',
                    btn: ['保存', '关闭'],
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.AcceptClick();
                    },
                    cancel: function (index) {
                        layer.close(index);
                    }
                });
            },
            OpenLine: function (id, object) {
                FlowDesignObject = object;
                LineId = id;
                var _line = object.$lineData[id];
                var _fromNode = object.$nodeData[_line.from];
                if (_fromNode.type == "shuntnode")
                {
                    dialogTop("前一个节点是分流节点无法设置流转条件", "error");
                    return false;
                }

                layer.open({
                    type: 2,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['800px', '450px'], //宽高
                    maxmin: true, //开启最大化最小化按钮
                    title: '流转条件设置',
                    content: '/FlowManage/FlowDesign/FlowLineForm',
                    btn: ['保存', '关闭'],
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.AcceptClick();
                    },
                    cancel: function (index) {
                        layer.close(index);
                    }
                });

            }
        });
    }
    function setFlowInfo(data)
    {
        FlowDesignPanel.loadData(data);
    }
    function callBackNode(id, data, _nodePramData)
    {
        nodePramData = _nodePramData;
        FlowDesignPanel.SetNodeEx(id, data);
    }
    function callBackLine(id, data)
    {
        FlowDesignPanel.SetLineEx(id, data);
    }
    function bindingFlow()
    {
        var _content = FlowDesignPanel.exportDataEx();
        if (_content == -1)
        {
            return false;
        }
        flowData["SchemeContent"] = JSON.stringify({ "Frm": frmData, "Flow": _content });
        return true;
    }
    /*=========流程设计（end）====================================================================*/

    /*=========创建完成（begin）==================================================================*/
    function finishbtn() {
        postData["EnabledMark"] = 1;
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        $.SaveForm({
            url: "../../FlowManage/FlowDesign/SaveForm?keyValue=" + keyValue,
            param: { "InfoEntity": JSON.stringify(postData), "ContentEntity": JSON.stringify(flowData), "shcemeAuthorizeData": shcemeAuthorizeData },
            loading: "正在保存数据...",
            success: function () {
                parent.layer.close(index);
            }
        })
    }
    /*=========创建完成（end）====================================================================*/
</script>

<div class="widget-body">
    <div id="wizard" class="wizard" data-target="#wizard-steps">
        <ul class="steps">
            <li data-target="#step-1" class="active"><span class="step">1</span>基本配置<span class="chevron"></span></li>
            <li data-target="#step-2"><span class="step">2</span>表单选择<span class="chevron"></span></li>
            <li data-target="#step-3"><span class="step">3</span>流程设计<span class="chevron"></span></li>
            <li data-target="#step-4"><span class="step">4</span>创建完成<span class="chevron"></span></li>
        </ul>
    </div>
    <div class="step-content wizard-step-content" id="wizard-steps">
        <div class="step-pane wizard-step-pane active" id="step-1">
            <div class="alert alert-danger" style="text-align: left; margin-bottom: 10px;">
                <i class="fa fa-warning alert-dismissible" style="position: relative; top: 1px; font-size: 15px; padding-right: 5px;"></i>
                请你创建流程信息，用于创建或修改流程！
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">流程基本信息配置</h3>
                </div>
                <div class="panel-body" >
                    <table class="form">
                        <tr>
                            <td class="formTitle">流程编号<font face="宋体">*</font></td>
                            <td class="formValue">
                                <input id="SchemeCode" type="text" class="form-control" placeholder="请输入流程编号" isvalid="yes" checkexpession="NotNull" />
                            </td>
                        </tr>
                        <tr>
                            <td class="formTitle">流程名称<font face="宋体">*</font></td>
                            <td class="formValue">
                                <input id="SchemeName" type="text" class="form-control" placeholder="请输入流程名称" isvalid="yes" checkexpession="NotNull" />
                            </td>
                        </tr>
                        <tr>
                            <th class="formTitle formTitle-top">
                                备注
                            </th>
                            <td class="formValue">
                                <textarea id="Description" class="form-control"></textarea>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
     
        <div class="step-pane" id="step-2">
            <div>
                <div id="FormFrmTree" class="border-right" style="width: 190px; float: left;"></div>
                <div style="width: 890px;float:right;margin-right:10px;">
                    <div class="alert alert-danger" style="text-align: left;margin-top:10px;margin-bottom:10px;">
                        <i class="fa fa-question-circle alert-dismissible" style="position: relative; top: 1px; font-size: 15px; padding-right: 5px;"></i>
                        注：1、请在左侧选择需要绑定表单。2、可编辑表单(此编辑只作用与当前流程)。
                    </div>

                    <div class="tab-content editview border" style="overflow-y:auto;overflow-x:hidden;">
                        <div class="editviewtitle" id="editfrm">
                            编辑表单
                        </div>
                        <div style="width:870px;display:none;" id="frmdesign"></div>
                        <div class="app_layout app_preview" id="frmpreview" style="display:none;"></div>
                        <div id="frmDefaulting" style="width:300px;margin:140px auto 0px;text-align:center;font-size:100px;color:#0FA74F"><i class="fa fa-table"></i><div style="font-weight: bold; font-size: 24px; color: #0FA74F;margin-top: 0px;">表单预览</div></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="step-pane" id="step-3">
            <div id="FlowPanel" style="margin: 0px;">
            </div>
        </div>
        <div class="step-pane" id="step-4">
            <div class="drag-tip">
                <i class="fa fa-check-circle"></i>
                <p >设计完成,请点击保存</p>
            </div>
        </div>
    </div>
    <div class="form-button" id="wizard-actions">
        <a id="btn_last" disabled class="btn btn-default btn-prev">上一步</a>
        <a id="btn_next" class="btn btn-default btn-next">下一步</a>
        <a id="btn_caogao" class="btn btn-info">保存草稿</a>
        <a id="btn_finish" disabled class="btn btn-success" onclick="finishbtn();">保存</a>
    </div>
</div>
<style>
    .editviewtitle {
        position: fixed !important;
        top: 117px;
        right: 11px;
        width: 50px;
        z-index: 1000;
    }
    .standtab-pane {
        width:224px;
        border-right:1px solid #ccc;
        float:left;
    }
    .SchemeAuthorizePanel {
        width:828px;
        float: left;
        display:none;
    }
    .card-box.active {
        background: url(../../Content/Images/item_close.png) right top no-repeat !important;
    }
</style>
